// pages/passRecovery/passRecovery.js

//获取app实例
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    time: 60, //定时器总秒数
    disable: true  //按钮状态

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.from == 'recover') {
      this.setData({
        from: 'recover' //判断是修改密码的最后一步，设置支付密码
      });
    }
  },
  onShow: function(){
    wx.request({
      url: app.baseURL + 'Mine&a=show_mobile&user_id=' + app.globalData.userId,
      success: (res) => {
        if (res.data.status == 1){
          this.setData({
            my_mobile: res.data.mobile
          })
        }
      }
    })
  },

  getMsgCode(e) {
    let time = this.data.time, //定时器秒数
      timer; //定时器
    const userId = app.globalData.userId;
    wx.request({
      url: app.baseURL + 'Mine&a=getcode&user_id=' + userId,
      success: (res) => {
        let data = res.data;
        if (data.status === 0) {
          console.log(data.info.http_status_code);
          if (data.info.http_status_code == 400) {
            console.log(data.info.msg);
            wx.showModal({
              title: '提示',
              content: data.info.msg,
            })
          } else {
            this.toast(data.info)
          }
        } else if (data.status === 1) {
          this.toast(data.info, true);
          timer = setInterval(() => {
            //倒计时到0时，清除定时器，按钮可点击状态
            if (time == 0) {
              clearInterval(timer);
              this.setData({
                time: 60,
                disable: true
              });
              //秒数递减  
            } else {
              time--;
              this.setData({
                time: time
              });
            }
          }, 1000);
          this.setData({
            disable: false
          });
        };
      }
    })
  },

  //跳转设置验证码
  formSubmit(e) {
    const userId = app.globalData.userId,
      code = e.detail.value.code,
      password = e.detail.value.password,
      passwordAgin = e.detail.value.passwordAgin;
    console.log(e.detail.value.code)
    //基本验证  
    if (!code) { this.toast('请输入验证码'); return false; }
    if (!password) { this.toast('请设置支付密码'); return false; }
    if (password.length != 6) { this.toast('支付密码长度为6'); return false; }
    if (password != passwordAgin) { this.toast('两次密码不一样，请重新输入'); return false; } 
    wx.request({
      url: app.baseURL + 'Mine&a=found_pay',
      data: {
        user_id: userId,
        code: code,
        pwd: passwordAgin
      },
      success: (res) => {
        let data = res.data;
        if (data.status === 0) {
          this.toast(data.info);
        } else if (data.status === 1) {
          this.toast(data.info, true);
          setTimeout(() => {
            wx.navigateBack({delta: 1})
          }, 1000);
        }
      }
    })
  },
  toast(msg, isico) {
    wx.showToast({
      title: msg,
      icon: isico ? 'success' : 'none'
    })
  }
})